package com.ychug.gateway.system;


import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.web.base.BaseController;
import com.ychug.framework.web.page.TableDataInfo;
import com.ychug.service.system.entity.SysRoleEntity;
import com.ychug.service.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 * 
 * @author dds
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController
{
    private String prefix = "modules/sys/role";

    @Autowired
    private ISysRoleService roleService;


    @GetMapping()
    public String role()
    {
        return prefix + "/viewRole";
    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysRoleEntity role)
    {
        startPage();
        List<SysRoleEntity> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }


    @PostMapping("/export")
    @ResponseBody
    public ResultAjaxHash export(SysRoleEntity role)
    {
//        List<SysRoleEntity> list = roleService.selectRoleList(role);
//        ExcelUtil<SysRoleEntity> utils = new ExcelUtil<SysRoleEntity>(SysRoleEntity.class);
        return null;//utils.exportExcel(list, "role");
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/addRole";
    }

    /**
     * 新增保存角色
     */

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public ResultAjaxHash addSave(SysRoleEntity role)
    {
        role.setCreateBy("admin");
       // ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(roleService.insertRole(role));

    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/editRole";
    }

    /**
     * 修改保存角色
     */

    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public ResultAjaxHash editSave(SysRoleEntity role)
    {
        role.setUpdateBy("admin");
        return toAjax(roleService.updateRole(role));
    }

    /**
     * 新增数据权限
     */
    @GetMapping("/rule/{roleId}")
    public String rule(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/rule";
    }

    /**
     * 修改保存数据权限
     */

    @PostMapping("/rule")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public ResultAjaxHash ruleSave(SysRoleEntity role)
    {
        role.setUpdateBy("admin");
        return toAjax(roleService.updateRule(role));
    }


    @PostMapping("/remove")
    @ResponseBody
    public ResultAjaxHash remove(String ids)
    {
        try
        {
            return toAjax(roleService.deleteRoleByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(SysRoleEntity role)
    {
        return roleService.checkRoleNameUnique(role);
    }

    /**
     * 校验角色权限
     */
    @PostMapping("/checkRoleKeyUnique")
    @ResponseBody
    public String checkRoleKeyUnique(SysRoleEntity role)
    {
        return roleService.checkRoleKeyUnique(role);
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree")
    public String selectMenuTree()
    {
        return prefix + "/tree";
    }
}